package com.qf.dao.impl;

import com.qf.dao.MyProductsDao;
import com.qf.entity.MyProducts;
import com.qf.entity.Products;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class MyProductsDaoImpl implements MyProductsDao {

    private QueryRunner queryRunner = new QueryRunner(JdbcUtils.ds);

    @Override
    public List<MyProducts> selectByUid(int uid) {
        try {
            return queryRunner.query("select * from t_myproducts where uid = ?",new BeanListHandler<>(MyProducts.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertProByUid(MyProducts products) {
        String sql = "insert into t_myproducts value(?,?,?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql,products.getId(), products.getName(), products.getUid(), products.getPrice(), products.getYieid(), products.getType(), products.getNum(), products.getAmount());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public MyProducts selectByID(int id) {
        try {
            return queryRunner.query("select * from t_myproducts where id = ?",new BeanHandler<>(MyProducts.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateMyProByID(double myNum, double myAmount, int id) {
        String sql = "update t_myproducts set num = ? , amount = ? where id = ?";
        try {
            return queryRunner.update(sql,myNum,myAmount,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
